import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
const wx = window["wx"];
import *as cc from "cc";
import DatasSpace from './Tool/DatasSpace';
import { MISSION_TYPE } from '../Cube/Levels';
import { showTips } from './Tips/Tips';
import { KuaishouAdAdapter } from './KuaishouAdAdapter';


@ccclass('SkdComm')
export default class SkdComm {
  //创建本类的全局调用变量
  public static _instance: SkdComm = null
  public static get Instance() {
    if (null == this._instance) {
      this._instance = new SkdComm();
    }
    return this._instance;
  }

  //将
  platformType = cc.sys.Platform.WECHAT_GAME;
  public gameClubButon = null;

  /**
   * @fn 分享
   * @time 2024 5 29 13:52
   * 
   */
  private shareTag: boolean = false;
  private shareFunc: Function = null;
  private shareTime: number = null;

  initShare() {
    if (cc.sys.platform != cc.sys.Platform.WECHAT_GAME) return;
    wx.onShow(() => {
      if (this.shareTag) {
        console.log('分享回调')
        let nowTime = Date.now();
        if ((nowTime - this.shareTime) > 2500) {
          showTips('分享成功')
          DatasSpace.Instance.addMission_Target(MISSION_TYPE.SHARE, 1);
          this.shareFunc && this.shareFunc();
        } else {
          showTips('分享失败')
        }
        this.shareTag = false;
        this.shareFunc = null;
        this.shareTime = 0;
      }
    })
  }

  shareTo(successCall?: Function) {
    // 快手环境下使用激励广告替代分享
    if (typeof (window as any).ks !== 'undefined') {
      console.log("快手环境：使用激励广告替代分享功能");
      KuaishouAdAdapter.Instance.simulateShare((success: boolean) => {
        if (success) {
          showTips('观看广告成功，获得分享奖励');
          DatasSpace.Instance.addMission_Target(MISSION_TYPE.SHARE, 1);
          if (successCall) successCall();
        } else {
          showTips('观看广告失败');
        }
      });
      return;
    }

    if (cc.sys.platform == cc.sys.Platform.WECHAT_GAME) {
      wx.shareAppMessage({
        templateId: '',
        query: "",
        success: function () {

        },
        fail: function () {

        }
      });
      this.shareTag = true;
      this.shareFunc = successCall;
      this.shareTime = Date.now();

    } else {
      console.log("非微信环境，模拟分享成功");
      successCall && successCall();
      DatasSpace.Instance.addMission_Target(MISSION_TYPE.SHARE, 1);
    }
  }

  /**
   * @fn 创建游戏圈按钮
   * @param node 
   * @time 2024 5 29 13:55
   */
  createGameClub(btnNode?: Node) {
    let texture_tem = null;

    if (cc.sys.platform == cc.sys.Platform.WECHAT_GAME) {

      if (!this.gameClubButon) {
        if (!btnNode) return;
        
        try {
          let sysInfo = wx.getSystemInfoSync();
          let windowSize = cc.view.getVisibleSize();
          let node_pos = btnNode.getPosition();
          //console.log('node_pos', node_pos)
          let leftRatio = (node_pos.x + windowSize.width / 2) / windowSize.width;
          let topRatio = 1 - (node_pos.y + windowSize.height / 2) / windowSize.height;
          //console.log("sysInfo", leftRatio, topRatio);

          let left = sysInfo.windowWidth * leftRatio;
          let top = sysInfo.windowHeight * topRatio;

          let nodeSize = btnNode.getComponent(cc.UITransform).contentSize;
          
          if (wx.createGameClubButton) {
            this.gameClubButon = wx.createGameClubButton({
              type: 'text',
              text: '',
              //icon: 'green',
              style: {
                left: left - nodeSize.width / 4,
                top: top - nodeSize.height / 4,
                width: nodeSize.width / 2,
                height: nodeSize.height / 2
              },
              // style: {
              //     left: 10,
              //     top: 55,
              //     width: 35,
              //     height: 35
              // },
              image: texture_tem,
            });
          } else {
            console.log('createGameClubButton API 不可用');
          }
        } catch (error) {
          console.log('创建游戏圈按钮失败:', error);
        }
      } else {
        try {
          this.gameClubButon.show();
        } catch (error) {
          console.log('显示游戏圈按钮失败:', error);
        }
      }
      // node.setPosition( this.gameClubButon.style.left, this.gameClubButon.style.top );
      // node.setPosition( -windowInfo.screenWidth/2 - lefts, windowInfo.screenHeight/2 + tops);
    }
  }

  hideGameClub() {
    try {
      if (this.gameClubButon) {
        this.gameClubButon.hide();
      }
    } catch (error) {
      console.log('隐藏游戏圈按钮失败:', error);
    }
  }

}


